package com.example.jetpackstu.logic

import com.example.jetpackstu.StuAppApplication
import com.example.jetpackstu.logic.db.AppDatabase
import com.example.jetpackstu.logic.entity.News

object Repository {

    private val appDatabase = AppDatabase.getDatabase(StuAppApplication.context)

    fun allNews() = appDatabase.newsDao().getAll()

    fun newsById(id: Long) = appDatabase.newsDao().getById(id)

    fun newsSave(title: String, content: String) {
        appDatabase.newsDao().insert(News(title = title, content = content))
    }

    fun newsUpdate(news: News) {
        appDatabase.newsDao().update(news)
    }

    fun newsDel(id: Long) {
        appDatabase.newsDao().deleteById(id)
    }

//    private fun <T> fire(context: CoroutineContext, block: suspend () -> Result<T>) =
//        liveData<Result<T>>(context) {
//            val result = try {
//                block()
//            } catch (e: Exception) {
//                Result.failure<T>(e)
//            }
//            emit(result)
//        }

}